
<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%>
<%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.io.*"%><%@page import="org.jdom.input.SAXBuilder,org.jdom.Document"%><%@page import="org.jdom.Element"%><%@page import="org.w3c.dom.NodeList"%>
<%
    System.out.println("滚石接口-上行同步");
	String syncDataSourceIp = request.getRemoteHost();
	String recvMsgType = "";
	String saveGwid = "";
	response.setContentType("text/plain");
	PrintWriter out1 = response.getWriter();

	InputStream in = request.getInputStream();
	StringBuffer provisionReq = new StringBuffer(512);
	byte[] buf = new byte[1024];
	int len = 0;
	try {
		while ((len = in.read(buf)) != -1) {
			String str = new String(buf, 0, len, "gb2312");
			provisionReq.append(str);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	in = new ByteArrayInputStream(provisionReq.toString().trim().getBytes("gb2312"));
	SAXBuilder builder = new SAXBuilder();
	try {
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		String rootElementName = root.getName();
		
		//解析节点
		String mobile = root.getChild("SRC_TERMID").getValue();          //手机号码
		String spNo = root.getChild("DST_SERVICECODE").getValue();
		String serviceType = root.getChild("SERVICEID").getValue();      //计费代码
		String linkid = root.getChild("TRANSACTIONID").getValue();
		String mo = root.getChild("CONTENT").getValue();              //手机上行内容
		String feeType = root.getChild("DATA_TYPE").getValue();
		String fee=root.getChild("FEE_CODE").getValue();
		String province=root.getChild("PROVINCE").getValue();
		String date=root.getChild("SENDTIME").getValue();

    	//指令处理,返回处理后的指令
    	String strMo = BillingUtil.getMo(spNo, mo);
    	System.out.println("滚石接口-上行同步：原始上行:"+mo+";处理后的上行："+strMo);
		//获取用户渠道号
		int channelId = BillingUtil.getUserChannel(spNo,strMo);
		//sp标识，类似于网关号
		int spid = BillingUtil.getUserSpId(spNo,strMo);
		//省份
		int provinceId = BillingUtil.getProvinceId(mobile);
	   
		//---------------扣量处理
		//得到CP渠道扣量比例数
	    int cpKouLiangShu = BillingUtil.getCpKouLiangBiLv(channelId);
		int cpKouLiang = 0;
		int ischeck = 0;
	    //得到CP渠道指令扣量比例数
	    int cpMoKouLiangShu = BillingUtil.getCpMoKouLiangBiLv(channelId,spid,strMo);
	    if(cpMoKouLiangShu!=0){
	    	cpKouLiang = cpMoKouLiangShu;
	    	int cpauoid = BillingUtil.getCpAutoId(channelId, spid, strMo);//查询cp对应的autoid，和下面进行区分，以免计数不对
			ischeck = BillingUtil.getCheckRecv(cpauoid,cpKouLiang);//获取是否要扣量
	    }else{
	    	cpKouLiang = cpKouLiangShu;
			ischeck = BillingUtil.getCheckRecv(channelId,cpKouLiang);//获取是否要扣量，这里直接用渠道号
	    }
	    //---------------
		
		StringBuilder sb = new StringBuilder();
	    sb.append("'");
	    sb.append(spid);
	    sb.append("',");
	    sb.append("'");
	    sb.append(mobile);
	    sb.append("',");
	    sb.append("'");
	    sb.append(spNo);
	    sb.append("',");
	    sb.append("");
	    sb.append("getDate()");
	    sb.append(",");
	    sb.append("'");
	    sb.append("msgid");
	    sb.append("',");
	    sb.append("'");
	    sb.append("mid");
	    sb.append("',");
	    sb.append("'");
	    sb.append("1");//cardtype
	    sb.append("',");
	    sb.append("'");
	    sb.append(channelId);//channel
	    sb.append("',");
	    sb.append("'");
	    sb.append("101");//prdid
	    sb.append("',");
	    sb.append("'");
	    sb.append("010");//telid
	    sb.append("',");
	    sb.append("'");
	    sb.append(linkid);//linkid
	    sb.append("',");
	    sb.append("'");
	    sb.append(mo);//上行内容
	    sb.append("'");
	    sb.append(",");
	    sb.append(provinceId);
	    sb.append(",");
	    sb.append(ischeck);
	    
	    SqlUtil.executeUpdate("insert into "+BillingUtil.getRecvDay()+"(areaid,org,dest,tm,msgid,mid,cardtype,channel,prdid,teleid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
	    date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());//200912121200
	    System.out.println("滚石接口-上行是否扣量："+ischeck+",cp:"+channelId+",spNo:"+spNo+",mo:"+mo+",渠道扣量比例："+cpKouLiangShu+",指令扣量比例："+cpMoKouLiangShu+",最终比例扣量："+cpKouLiang);
	    
	    if(ischeck == 0) {
	    	System.out.println("滚石-上行同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5));
	    	String http = BillingUtil.getMoUrlByChannel(channelId);
	    	if(http!=null&&http.length()>0) {
	    		StringBuilder moUrl = new StringBuilder(http);
	    		if(moUrl.indexOf("?")>0){
	    			moUrl.append("&userPhone=");
	    		}else{
	    			moUrl.append("?userPhone=");
	    		}
		    	moUrl.append(mobile);
		    	moUrl.append("&spNo=");
		    	moUrl.append(spNo);
		    	moUrl.append("&linkId=");
		    	moUrl.append(linkid);
		    	moUrl.append("&mo=");
		    	moUrl.append(mo);
		    	moUrl.append("&date=");
		    	moUrl.append(date);
			    System.out.println("滚石-上行同步url:"+moUrl.toString());
			    HttpUtil.getHtmlSource(moUrl.toString(),"UTF-8");
	    	}
	    	
	    }
		String xmlStr="<?xml version=\"1.0\" encoding=\"gb2312\" ?><MASP><RESULT>200</RESULT><CONTENT></CONTENT></MASP>";
		out1.print(xmlStr);
		out1.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			out1.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
%>

